﻿package view.mainUI.td
{
    import cmd.*;
    import com.core.*;
    import com.sh.game.consts.*;
    import com.sh.game.net.*;
    import com.sh.game.util.*;
    import consts.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;
    import manager.*;
    import pluginfw.nio.*;
    import uiresource.instances.ui.*;
    import view.*;
    import view.light.*;
    import view.message.*;
    import view.uilt.*;

    public class TdMenu extends TDuiUI
    {
        private const tid1:Array;
        private const tid:Array;
        private var sp:Sprite;
        private var tdtime:int;
        private var _timeID:uint = 0;
        private var _reshowID:uint = 0;
        private var shownext:Boolean = true;
        private var shownext1:Boolean = true;
        private var _tcount:int = 0;
        private var _tcount1:int = 0;
        private var oldnum:int = 0;
        private var playing:Boolean = false;
        private var _exp:int = 0;
        private var _lighttime:uint = 0;

        public function TdMenu()
        {
            this.tid1 = [240202035, 5 + "W金币"];
            this.tid = [240202034, 10 + "W累积经验"];
            this.sp = new Sprite();
            this.initEvent();
            this.addChild(this.sp);
            this.sp.y = 100;
            this.sp.visible = true;
            this.visible = false;
            ModelProxy.change("guideitem", "guideid" + GuideType.GUIDE_DJSW, btn_zhaohuan);
            ModelProxy.change("guideitem", "guideid" + GuideType.GUIDE_DJSW1, btn_zhaohuan);
            ModelProxy.change("guideitem", "guideid" + GuideType.GUIDE_DJSW2, btn_zhaohuan);
            ModelProxy.change("guideitem", "guideid" + GuideType.GUIDE_DJSW3, btn_zhaohuan);
            ModelProxy.change("guideitem", "guideid" + GuideType.GUIDE_DJSW4, btn_zhaohuan);
            ModelProxy.change("guideitem", "guideid" + GuideType.GUIDE_KSSG, shuaguaibtn);
            return;
        }// end function

        private function initEvent() : void
        {
            btn_zhaohuan.addEventListener(MouseEvent.CLICK, this.btn_zhaohuanClickHandler);
            btn_zhaohuan1.addEventListener(MouseEvent.CLICK, this.btn_zhaohuan1ClickHandler);
            shuaguaibtn.addEventListener(MouseEvent.CLICK, this.shuaguaibtnClickHandler);
            btn_zhaohuan.addEventListener(MouseEvent.ROLL_OVER, this.shuaguaibtnRollOver1);
            btn_zhaohuan.addEventListener(MouseEvent.ROLL_OUT, this.shuaguaibtnRollOut);
            btn_zhaohuan1.addEventListener(MouseEvent.ROLL_OVER, this.shuaguaibtnRollOver);
            btn_zhaohuan1.addEventListener(MouseEvent.ROLL_OUT, this.shuaguaibtnRollOut);
            UIGlobel.pointsDic["zaohuan"] = new Point(btn_zhaohuan.x, btn_zhaohuan.y);
            return;
        }// end function

        protected function shuaguaibtnRollOver1(event:MouseEvent) : void
        {
            ModelProxy.change("tips", "normal", "购买低级守卫，需要消耗" + this.tid[1] + "或消耗低级守卫令牌一枚（优先消耗道具）");
            ModelProxy.change("zone", "TDaddEclipse", true);
            return;
        }// end function

        protected function shuaguaibtnRollOver(event:MouseEvent) : void
        {
            ModelProxy.change("zone", "TDaddEclipse", true);
            ModelProxy.change("tips", "normal", "  购买高级守卫，需要消耗" + this.tid1[1] + "或消耗高级守卫令牌一枚（优先消耗道具）");
            return;
        }// end function

        public function show(param1:Boolean = true) : void
        {
            this.tdtime = ModelProxy.getValue("data", "tdtime");
            box_tishi.visible = false;
            if (this._timeID > 0)
            {
                clearInterval(this._timeID);
                this._timeID = 0;
            }
            if (param1)
            {
                this.visible = param1;
                GuideView.getInstance().doGuide(GuideType.GUIDE_DJSW);
            }
            return;
        }// end function

        public function close() : void
        {
            this.visible = false;
            return;
        }// end function

        protected function shuaguaibtnRollOut(event:MouseEvent) : void
        {
            ModelProxy.change("zone", "TDaddEclipse", false);
            ModelProxy.change("tips", "state", "close");
            return;
        }// end function

        protected function shuaguaibtnClickHandler(event:MouseEvent) : void
        {
            var _loc_2:* = new Output();
            this.sendBuffer(CMDStage.StartTD, _loc_2);
            clearInterval(this._timeID);
            this._timeID = 0;
            box_tishi.visible = false;
            ModelProxy.change("guide", "hide", GuideType.GUIDE_KSSG);
            return;
        }// end function

        protected function btn_zhaohuan1ClickHandler(event:MouseEvent) : void
        {
            var event:* = event;
            if (this._tcount1 + ItemUilt.getAllCount(this.tid1[0]) <= 0 && this.shownext1)
            {
                Alert2.show("将消耗" + this.tid1[1] + "自动购买高级守卫，是否购买？", "", ["确定", "取消"], function (param1:int, param2:Object) : void
            {
                if (param1 == 100)
                {
                    shownext1 = false;
                    buy(1);
                }
                else if (param1 == 101)
                {
                    shownext1 = false;
                }
                if (param1 == 0)
                {
                    buy(1);
                }
                return;
            }// end function
            , null, false, true, true);
            }
            else
            {
                this.buy(1);
            }
            return;
        }// end function

        protected function btn_zhaohuanClickHandler(event:MouseEvent) : void
        {
            var event:* = event;
            if (this._tcount + ItemUilt.getAllCount(this.tid[0]) <= 0 && this.shownext)
            {
                Alert2.show("将消耗" + this.tid[1] + "自动购买低级守卫，是否购买？", "", ["确定", "取消"], function (param1:int, param2:Object) : void
            {
                if (param1 == 100)
                {
                    shownext = false;
                    buy(0);
                }
                else if (param1 == 101)
                {
                    shownext = false;
                }
                if (param1 == 0)
                {
                    buy(0);
                }
                return;
            }// end function
            );
            }
            else
            {
                this.buy(0);
            }
            return;
        }// end function

        private function buy(param1:int) : void
        {
            var _loc_2:* = ModelProxy.getValue("data", "SuperTF");
            if (_loc_2 == 15)
            {
                WarningManager.show(37);
                return;
            }
            var _loc_3:* = new Output();
            _loc_3.writeShort(param1);
            ModelProxy.change("data", "SuperTF", param1);
            this.sendBuffer(CMDStage.CreateSuperTF, _loc_3);
            clearInterval(this._timeID);
            this._timeID = 0;
            box_tishi.visible = false;
            return;
        }// end function

        private function sendBuffer(param1:int, param2:Output) : void
        {
            Server.instance.sendBuffer(102, param1, param2.getBuffer(), 0, ConnectType.SYSTEMZONE);
            return;
        }// end function

        public function update(param1:Object) : void
        {
            if (param1 == null)
            {
            }
            else if (param1.tid == 0)
            {
                this._tcount = param1.tsize;
            }
            else if (param1.tid == 1)
            {
                this._tcount1 = param1.tsize;
            }
            var _loc_2:* = UserData.getBagData();
            if (_loc_2 == null)
            {
                return;
            }
            label1.text = "剩余" + (this._tcount + ItemUilt.getAllCount(this.tid[0]));
            label2.text = "剩余" + (this._tcount1 + ItemUilt.getAllCount(this.tid1[0]));
            return;
        }// end function

        public function gettdexp(param1:int) : void
        {
            this._exp = param1;
            return;
        }// end function

        public function overDo() : void
        {
            this.playing = false;
            if (this.oldnum != 0)
            {
                this.playing = true;
                this.oldnum = 0;
            }
            return;
        }// end function

        public function showlight() : void
        {
            if (this._lighttime > 0)
            {
                clearTimeout(this._lighttime);
                this._lighttime = 0;
            }
            this._lighttime = setTimeout(this.cclight, 800);
            return;
        }// end function

        private function cclight() : void
        {
            this._lighttime = 0;
            LightFactory.showlight(btn_zhaohuan, 0);
            return;
        }// end function

        public function updateTdStart(param1:Boolean) : void
        {
            this.shuaguaibtn.disabled = param1;
            return;
        }// end function

    }
}
